<?php

namespace app\apishop\controller;

use app\admin\model\patrol\Inspection;
use app\admin\model\shop\ShopClass;
use app\apishop\validate\ShopValidate;
use app\common\controller\Api;
use tekintian\TekinQR;
use think\Config;
use think\Exception;

/**
 * 商户
 */
class Shop extends Api
{

    protected $noNeedLogin = ['class_list'];
    protected $noNeedRight = '*';

    protected $failException = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\shop\User();
        $this->modelType =  new ShopClass();
        $this->modelInspection = new Inspection();
        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }
    }


    /**
     * 列表
     * @return void
     */
    public function list()
    {
        $input = input();
        try {
            $pageArr = [
                'page' => 1,
                'page_limit' => 30,
            ];
            if (!empty($input['keyWork'])){
                $where['name'] = ['like','%'.$input['keyWork'].'%'];
            }
            if (!empty($input['id'])){
                $where['id'] = $input['id'];
            }
            $pageArr = array_merge($pageArr, $input);
            $where['istrue'] = 2;
            $data = $this->model->where($where)
                ->order('id desc')
                ->field('*')
                ->paginate(['page' => $pageArr['page'] ?: 1, 'list_rows' => $pageArr['page_limit'] ?: 10]);
            foreach ($data->items() as &$v){
                $v->user_shop_class_name = $this->modelType->where('id',$v['user_shop_class_id'])->value('name') ?? '';
            }
            $this->success('ok', $data);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }


    /**
     * 用户类别列表
     * @return void
     */
    public function class_list()
    {
        $input = input();
        try {
            $where = [];
            if (!empty($input['keyWork'])){
                $where['name'] = ['like','%'.$input['keyWork'].'%'];
            }
            $data = $this->modelType->where($where)
                ->order('id desc')
                ->field('*')
                ->select();

            $this->success('ok', $data);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }


    /**
     * 问题上报 列表
     * @return void
     */
    public function task_list()
    {
        $input = input();
        $user_info = $this->auth->getUserinfo();
        try {
            $pageArr = [
                'page' => 1,
                'page_limit' => 30,
            ];
            $pageArr = array_merge($pageArr, $input);


            //根据用户所在的管辖
            $inspection_ids = $this->modelInspection->where('user_staff_class_id', $user_info['user_staff_class_id'])
                ->column('id', 'id');
            $where['patrol_inspection_id'] = ['in', $inspection_ids];

            if (!empty($input['keyWord'])) {
                $where['name'] = ['like', '%' . $input['keyWord'] . '%'];
            }


            $data = $this->model->where($where)
                ->where('istrue', 2)
                ->order('id desc')
                ->field('id,name,license,username,coverimage,status,istype,lnglat')
                ->paginate(['page' => $pageArr['page'] ?: 1, 'list_rows' => $pageArr['page_limit'] ?: 10]);
            $this->success('ok', $data);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }



    //生成二维码
    public function qrcode()
    {
        $url = input('post.');
        if (empty($url['url'])) {
            $this->error('无效的二维码');
        }
        $user = $this->auth->getUserinfo(2);
        $mer_id = $user['id'];
        $mer_name = $user['name'];
        $url = $url['url'] . "?mer_id={$mer_id}&mer_name=$mer_name";
        $public = 'public';
        $_tmp_qr_file = '/uploads/qrcode/' . $mer_id . '.png';
        $top = '../';
        $file_qrcode = $top . 'public/uploads/qrcode';
        if (!is_dir($file_qrcode)) {
            mkdir($file_qrcode);
        }
        $_tmp_qr_files = $top . $public . $_tmp_qr_file;
        TekinQR::getQRImg($url, 10, cdnurl($user['coverimage']), 2, $_tmp_qr_files);
        $this->success('请求成功', ['url' => cdnurl($_tmp_qr_file, true)]);
    }



    /**
     * 修改密码
     * @return void
     */
    public function resetpwd()
    {
        $input = input('post.');
        if ($input['newpassword'] != $input['newpassword1']) {
            $this->error('原密码与确认密码不一致');
        }

        $ret = $this->auth->changepwd($input['newpassword'], $input['password'], false);
        if ($ret) {
            $this->success(__('密码修改成功请重新登录'));
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * 修改信息
     * @return void
     */
    public function set_filed(){
        $input = input('post.');
        $id = $this->auth->getUserinfo(2)['id'];
        try {
            $this->validate($input , ShopValidate::class.'.set_filed');
            $data = [$input['key'] => $input['value']];
            $this->model->where('id',$id)->update($data);
            $this->success('保存成功');
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

}